package com.project.medical.sys.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.project.medical.sys.common.utils.PageUtils;
import com.project.medical.sys.common.utils.Query;
import com.project.medical.sys.entity.CheckOutEntity;
import com.project.medical.sys.entity.DoctorEntity;
import com.project.medical.sys.service.CheckOutService;
import com.project.medical.sys.service.DoctorService;
import com.project.medical.sys.vo.PatientVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.project.medical.sys.dao.PatientDao;
import com.project.medical.sys.entity.PatientEntity;
import com.project.medical.sys.service.PatientService;
import org.springframework.transaction.annotation.Transactional;


@Service("patientService")
public class PatientServiceImpl extends ServiceImpl<PatientDao, PatientEntity> implements PatientService {
    @Autowired
    DoctorService doctorService;
    @Autowired
    CheckOutService checkOutService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<PatientEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(key)){
            wrapper.eq("id",key).or().like("name",key);
        }
        IPage<PatientEntity> page = this.page(
                new Query<PatientEntity>().getPage(params),
                wrapper
        );
        List<PatientEntity> records = page.getRecords();
         PageUtils pageUtils = new PageUtils(page);
        List<PatientVo> collect = records.stream().map(patientEntity -> {
            PatientVo patientVo = new PatientVo();
            BeanUtils.copyProperties(patientEntity, patientVo);
            DoctorEntity byId = doctorService.getById(patientEntity.getDoctorId());
            if (byId!=null){
                patientVo.setDoctorName(byId.getName());
            }
            return patientVo;
        }).collect(Collectors.toList());
        pageUtils.setList(collect);


        return pageUtils;
    }

    @Override
    @Transactional
    public boolean removePatientByIds(List<Integer> asList) {
        ArrayList<CheckOutEntity> checkOutEntities = new ArrayList<>();
        for (int i :
                asList) {
            PatientEntity patientEntity = baseMapper.selectById(i);
            CheckOutEntity checkOutEntity = new CheckOutEntity();
            checkOutEntity.setDoctorId(Integer.parseInt(patientEntity.getDoctorId()));
            checkOutEntity.setName(patientEntity.getName());
            checkOutEntity.setOutTime(new Date());
            checkOutEntity.setPatientId(patientEntity.getId());
            checkOutEntities.add(checkOutEntity);
        }
        boolean b = checkOutService.saveBatch(checkOutEntities);
        int i = baseMapper.deleteBatchIds(asList);
        return b && i>0;
    }

}